﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using RealtyManagementSystem.DAL;

namespace RealtyManagementSystem.Web.Householders
{
    public partial class SubmitRepairIssue : System.Web.UI.Page
    {
        protected Guid AuthorId
        {
            get
            {
                object strId = this.ViewState["authorId"];
                if (strId != null)
                    return new Guid(strId.ToString());
                return new Guid();
            }
            set
            {
                this.ViewState["authorId"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindAuthor();
                this.lblCreateDate.Text = DateTime.Now.ToShortTimeString();
            }
        }

        public void BindAuthor()
        {
            var user = Membership.GetUser();
            this.lblCreater.Text = user.UserName;
            AuthorId = new Guid(user.ProviderUserKey.ToString());
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            using (var contxt = new realtydbEntities())
            {
                var repIssue = new RepairIssue()
                  {
                      CreaterId = AuthorId,
                      RepairItemDesc = this.txtDescription.Text,
                      CreateDate = DateTime.Now,
                      StatusId = 2,
                      AcceptDate = DateTime.MaxValue,
                      AccepterId = AuthorId
                  };

                contxt.AddObject("RepairIssues", repIssue);
                contxt.SaveChanges();
            }

            this.txtDescription.Text = "";
            GridView1.DataBind();
            Page_Load(sender, e);
        }
    }//end class
}